package hr.tvz.lab.tom.malwaredb.list;

import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.Select;

import java.util.List;

import hr.tvz.lab.tom.malwaredb.R;
import hr.tvz.lab.tom.malwaredb.add.AddActivity;
import hr.tvz.lab.tom.malwaredb.entity.MalwareObject;
import hr.tvz.lab.tom.malwaredb.entity.MalwareObject_Table;

public class ListActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<MalwareObject> malwareObjectList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Intent intent = getIntent();
        // If activity was launched from search, display only specific objects.
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            // Attempt to add prefixes like "this:query" to search specific values. It will break with multiple prefixes.
            if (query.contains(":")) {
                String[] querySplit = query.split(":");
                switch (querySplit[0]) {
                    case "type":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.type.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "platform":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.platform.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "source":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.sourceLanguage.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "filetype":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.fileType.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "size":
                        // Parsing mess.
                        Integer sizeNumber;
                        try {
                            sizeNumber = Integer.parseInt(querySplit[1]);
                        } catch (NumberFormatException e) {
                            sizeNumber = -1;
                        }
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.size.is(sizeNumber)).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "author":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.author.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "origin":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.origin.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "date":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.dateDiscovery.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "alias":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.aliases.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    case "description":
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.description.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                        break;
                    // If something else is prefix, ignore it and treat it like searching for name.
                    default:
                        malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.name.like("%" + querySplit[1] + "%")).orderBy(MalwareObject_Table.name, true).queryList();
                }
            } else {
                malwareObjectList = new Select().from(MalwareObject.class).where(MalwareObject_Table.name.like("%" + query + "%")).orderBy(MalwareObject_Table.name, true).queryList();
            }
            // If there is no search, simply load everything.
        } else {
            malwareObjectList = new Select().from(MalwareObject.class).orderBy(MalwareObject_Table.name, true).queryList();
        }

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        if (recyclerView != null) {
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(new RecyclerViewAdapter(malwareObjectList));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add:
                // Launch activity for adding objects.
                Intent intent = new Intent(this, AddActivity.class);
                startActivityForResult(intent, RESULT_FIRST_USER);
                return true;
            case R.id.menu_search:
                // Start searching.
                onSearchRequested();
                return true;
            case R.id.menu_load_sample:
                // Add samples. It's impossible to get data from single remote encyclopedia due to incomplete data and mess.
                String[] sample_name_list = getResources().getStringArray(R.array.name_sample);
                String[] sample_type_list = getResources().getStringArray(R.array.type_sample);
                String[] sample_platform_list = getResources().getStringArray(R.array.platform_sample);
                String[] sample_source_list = getResources().getStringArray(R.array.source_sample);
                String[] sample_filetype_list = getResources().getStringArray(R.array.filetype_sample);
                String[] sample_size_list = getResources().getStringArray(R.array.size_sample);
                String[] sample_author_list = getResources().getStringArray(R.array.author_sample);
                String[] sample_origin_list = getResources().getStringArray(R.array.origin_sample);
                String[] sample_date_list = getResources().getStringArray(R.array.date_sample);
                String[] sample_aliases_list = getResources().getStringArray(R.array.aliases_sample);
                String[] sample_links_list = getResources().getStringArray(R.array.links_sample);
                String[] sample_description_list = getResources().getStringArray(R.array.description_sample);

                Delete.table(MalwareObject.class);
                for (Integer i = 0; i < sample_name_list.length; i++) {
                    MalwareObject sample = new MalwareObject(i,
                            sample_name_list[i],
                            sample_type_list[i],
                            sample_platform_list[i],
                            sample_source_list[i],
                            sample_filetype_list[i],
                            Integer.parseInt(sample_size_list[i]),
                            sample_author_list[i],
                            sample_origin_list[i],
                            sample_date_list[i],
                            sample_aliases_list[i],
                            sample_links_list[i],
                            sample_description_list[i]);
                    sample.save();
                }
                malwareObjectList = new Select().from(MalwareObject.class).orderBy(MalwareObject_Table.name, true).queryList();
                if (recyclerView != null) {
                    recyclerView.setAdapter(new RecyclerViewAdapter(malwareObjectList));
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // On successful addition or editing, refresh the list.
        if (resultCode == RESULT_OK) {
            malwareObjectList = new Select().from(MalwareObject.class).orderBy(MalwareObject_Table.name, true).queryList();
            recyclerView.setAdapter(new RecyclerViewAdapter(malwareObjectList));
        }
    }
}
